/**
 * @projectName: agriculture-smart-systems
 * @author: 小飞
 * @description: TODO
 * @date: 2023/12/20 13:37
 * @version: 1.0
 */
package com.fyt.service.enclosure.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.dto.enclosure.EditEnclosureDao;
import com.fyt.dto.enclosure.GetEnclosureDao;
import com.fyt.entity.Enclosure;
import com.fyt.entity.Pasture;
import com.fyt.mapper.EnclosureMapper;
import com.fyt.mapper.PastureMapper;
import com.fyt.service.enclosure.EnclosureService;
import com.fyt.service.pasture.PastureService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import java.util.Objects;
import java.util.Optional;

@Service
public class EnclosureServiceImpl extends ServiceImpl<EnclosureMapper ,Enclosure> implements EnclosureService {
    @Autowired
    private EnclosureMapper enclosureMapper;

    /**
     *根据前端传过来的牧场id,圈舍名,圈舍状态查询圈舍
     * @param map 前端传过来的信息
     * @return Result 封装状态码以及数据对象的信息
     */
    @Override
    public Result selectEnclosurePage(Map<String,String> map) {
        //创建一个optional的对象,调用参数可以为空的方法,判断如果不为空则进行转换,为空则跳过.map()这一句直接到给默认值
        int pageNumber = Optional.ofNullable(map.get("pageNumber"))
                .map(Integer::parseInt)
                .orElse(1);
        int pageSize = Optional.ofNullable(map.get("pageSize"))
                .map(Integer::parseInt)
                .orElse(10);
        Integer pastureCode = Integer.parseInt(map.get("pastureCode"));
        Page<Enclosure> enclosurePage = new Page<>(pageNumber,pageSize);
        QueryWrapper<Enclosure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_code", pastureCode);
        queryWrapper.orderByAsc("p_code");
               if(map.get("enclosureName")!=null){
                   queryWrapper.like("enclosure_name",map.get("enclosureName"));
               }
               if (map.containsKey("enable")) {
                    String enableValue = map.get("enable");
                    if (enableValue != null && !enableValue.isEmpty()) {
                        queryWrapper.eq("enable", Integer.parseInt(enableValue));
                    }
        }
        List<Enclosure> enclosures = enclosureMapper.selectPage(enclosurePage,queryWrapper).getRecords();

        if (!enclosures.isEmpty()) {
            for(int i=0;i<enclosures.size();i++){

            }
        }
        return new Result(enclosures.isEmpty()?null:enclosures, enclosures.isEmpty()?Code.get_err:Code.get_ok,enclosures.isEmpty()?"获取失败或者数据为空!":"获取成功!");
    }
    /**
     *修改建设信息的实现类
     * @param eId 圈舍id
     * @param editEnclosureDao 修改的暂存实体类
     * @return 返回修改后的状态码信息
     */
    @Override
    public Result updateOneEnclosure(Integer eId, EditEnclosureDao editEnclosureDao) {
        QueryWrapper<Enclosure> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Enclosure> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("enclosure_name",editEnclosureDao.getEnclosureName());
        if (enclosureMapper.selectOne(queryWrapper1)!=null){
            return new Result(null,Code.get_err,"该牧场下圈舍名已存在!");
        }
        queryWrapper.eq("e_id",eId);
        Enclosure enclosure = Enclosure.builder()
                .enclosureName(editEnclosureDao.getEnclosureName())
                .build();
        int result = enclosureMapper.update(enclosure,queryWrapper);
        return new Result(null,result>0?Code.get_ok:Code.get_err,result>0?"修改成功!":"修改失败或者未产生修改动作!");
    }


    /**
     * 新增圈舍的实现类
     * @param map 前端传过来的信息
     * @return
     */
    @Override
    public Result createOneEnclosure(Map<String, String> map){
        if (map.get("pastureCode") != null && map.get("enclosureName") != null) {
            int pastureCode = Integer.parseInt(map.get("pastureCode"));
            String enclosureName = map.get("enclosureName");
            QueryWrapper<Enclosure> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("p_code", pastureCode).eq("enclosure_name", enclosureName);
            Long existCount = enclosureMapper.selectCount(queryWrapper);
            if (existCount == 0) {
                Enclosure enclosure = Enclosure.builder().enclosureName(enclosureName).pastureCode(pastureCode).build();
                int result = enclosureMapper.insert(enclosure);
                return new Result(null, result > 0 ? Code.get_ok : Code.get_err, result > 0 ? "新增成功!" : "新增异常!");
            } else {
                return new Result(null, Code.get_err, "该牧场下已存在同名圈舍!");
            }
        } else {
            return new Result(null, Code.get_err, "牧场名或圈舍名为空!");
        }
    }

    /**
     * 修改圈舍的使用权限的实现类
     * @param eId 圈舍的id
     * @param enable 圈舍的权限
     * @return
     */
    @Override
    public Result deleteOneEnclosure(Integer eId,Integer enable) {
        QueryWrapper<Enclosure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("e_id",eId);
        Enclosure enclosure  = enclosureMapper.selectOne(queryWrapper);
        if(enclosure!=null){
            if (enclosure.getInventory()>0&&enable==0){
                return new Result(null,Code.get_err,"该圈舍下还有未出栏的畜牧,不可停用!");
            }
            if(enable == 1){
                enable = 0;
            }else{
                enable = 1;
            }
            if (Objects.equals(enclosure.getEnable(), enable)){
                return new Result(null,Code.get_err,"该圈舍状态已为最新,不需要更新!");
            }
            Enclosure enclosure1 =  Enclosure.builder().eId(eId).enable(enable).build();
            int result = enclosureMapper.updateById(enclosure1);
            return new Result(null,result>0?Code.get_ok:Code.get_err,result>0?"更改成功!":"更改失败!");
        }else{
            return new Result(null,Code.get_err,"信息错误,请稍后再试!");
        }
    }

    @Override   //获取指定牧场下未禁用圈舍
    public List<Enclosure> getEnclosureListByPastureId(String pastureId) {
        QueryWrapper<Enclosure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_code",pastureId);
        queryWrapper.eq("enable","1");
        List<Enclosure> enclosures = enclosureMapper.selectList(queryWrapper);
        return enclosures;
    }
}
